package com.data.csv;

import java.io.File;
import java.sql.Timestamp;
import java.util.*;

import org.hibernate.Session;
import org.hibernate.Transaction;

import tool.JsonTool;
import tool.TimeTool;

import com.base.bean.*;
import com.base.dao.*;
import com.base.service.UserService;

public class DakaDeal {
	
	private UserDakaDao dkDao= null;
	private UserDao udao =null;

	
	// 生成当月考勤统计报表
	public static void main(String[] args) {
		DakaDeal test = new DakaDeal();
		test.genKQCsv("/Users/chz/Desktop/qk-four-8.csv",2016,8);

	}
	
	public DakaDeal(){
		udao = (UserDao) _BaseDao.ac.getBean("UserDao");
		dkDao = (UserDakaDao)_BaseDao.ac.getBean("UserDakaDao");
	}
	

	
	// 设置非工作日
	public String[] getOffDays(){
		String s="6,7,13,14,20,21,27,28";
		return s.split(",");
	}
	
	//对该用户 针对每个工作日 进行am pm打卡记录的汇集
	public KQbean getKQbean(Integer uid,int year,int month){
		KQbean bean = new KQbean();
		User user = udao.findByHql("from User where uid="+uid).get(0);
		bean.setUid(uid);
		bean.setName(user.getTruename());
		bean.setCompany(user.getCompany());
		bean.setProvince(user.getProvince());
		
		
		String[] qks = bean.getKqs();
		qks[0]="N";
		
		String[] offDays = this.getOffDays();
		for(String s:offDays){
			Integer i= new Integer(s);
			qks[i]="N";
		}
		
		
		
		
		
		List<UserDaka> list = this.getDakaList(uid, year,month);
		
		for(UserDaka r:list){
			int date = this.getDate(r.getCreated());
			int checkAPM =this.checkAmOrPm(r.getCreated());
			String s =qks[date];
			if(s.equals("N"))
				continue;
			if(checkAPM==1)
				s=s+"A";
			else if(checkAPM==2)
				s=s+"B";
			qks[date]=s;
				
		}
		bean.setKqs(qks);
			
		
		
		return bean;
	}
	
	
	public List<UserDaka> getDakaList(int uid,int year ,int month){
		Timestamp start = TimeTool.generateTime(year, month, 1);
		Timestamp end = TimeTool.generateTime(year, month+1, 1);
		String hql = "from UserDaka where checked=1 and uid= :uid and created > :start and created < :end";
	
		Session session =null;
		session =  this.dkDao.getSessionFactory().getCurrentSession();
		Transaction tx = session.beginTransaction();
		List<UserDaka> list = session.createQuery(hql).setInteger("uid", uid)
			.setTimestamp("start", start)
			.setTimestamp("end", end).list();
		
		tx.commit();
		return list;
		
		
	}
	
	
	
	public static int getDate(Timestamp time){
		Calendar calendar = TimeTool.convertCal(time);
		int date = calendar.get(5);
		return date;
	}
	
	public  int checkAmOrPm(Timestamp time){
		Calendar calendar = TimeTool.convertCal(time);
		int hour = calendar.get(Calendar.HOUR_OF_DAY);
		if(hour<=12)
			return 1;
		else
			return 2;
	}
	
	
	
	public void genKQCsv(String filePath,int year,int month){
		// 导出汇发考勤结果文件
		List<User> users =  udao.findByHql("from User where state=1 and company='b'");
		List<KQbean> beans = new ArrayList<KQbean>();
		for(User u:users){
			beans.add(this.getKQbean(u.getUid(),year,month));
		}
		
		List<String> dataList =  new ArrayList<String>();
		for(KQbean bean:beans){
			dataList.add(bean.toString());
		}
		tool.CSVUtils.exportCsv(new File(filePath), dataList);
	}
	
	
//	tool.CSVUtils.exportCsv(new File(filePath), list);
	


}
